package com.zf.spring.batch;

import java.util.HashMap;
import java.util.Map;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Runner {
	public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		JobLauncher jobLauncher = (JobLauncher) applicationContext.getBean("jobLauncher");
		// 每次执行传入不同的参数。否则会被spring batch认为是重复跑批而拒绝执行！
		Map<String, JobParameter> params = new HashMap<String, JobParameter>();
		JobParameter jobParameter = new JobParameter(String.valueOf(System.nanoTime()));
		params.put("random", jobParameter);
		
		Job job = (Job) applicationContext.getBean("customJob");
		jobLauncher.run(job, new JobParameters(params));
		
		job = (Job) applicationContext.getBean("ioSampleJob");
		jobLauncher.run(job, new JobParameters(params));
		
	}
}
